package cn.wen.eeayjoke.dialog;

import android.app.Activity;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.util.TypedValue;

import cn.wen.base.dialog.QuickBuilder;
import cn.wen.base.dialog.QuickDialog;
import cn.wen.eeayjoke.R;


/**
 * 进度的Dialog
 */

public class LoadingDialog {

    private final QuickDialog mDialog;
    private Activity mActivity;

    public LoadingDialog(Activity activity) {
        this.mActivity = activity;
        mDialog = QuickBuilder.create(activity)
                .setContentView(R.layout.dialog_loading)
                .setWidth(dp2px(120))
                .setWidthScale(1.0f)
                .setHeight(dp2px(120))
                .create();
    }

    public int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, mActivity.getResources().getDisplayMetrics());
    }

    public void show(String text) {
        if (!TextUtils.isEmpty(text)) {
            mDialog.setText(R.id.tv_loading, text);
        }
        mDialog.show();
    }

    public void show() {
        mDialog.show();
    }

    public void dismiss() {
        mDialog.dismiss();
        mActivity = null;
    }

    public void setOnDismissListener(DialogInterface.OnDismissListener listener) {
        if (listener != null) {
            mDialog.setOnDismissListener(listener);
        }
    }
}
